package com.stox.charting.ui.swing.model.price;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import com.stox.charting.ui.swing.model.Crosshair;
import com.stox.charting.ui.swing.model.DefaultAxis;
import com.stox.charting.ui.swing.model.Grid;
import com.stox.charting.ui.swing.model.Orientation;
import com.stox.charting.ui.swing.model.indicator.BarChart;
import com.stox.charting.ui.swing.view.ChartView;
import com.stox.charting.ui.swing.view.ChartingInternalFrameView;
import com.stox.core.model.Bar;
import com.stox.core.model.IBar;

public class PriceChartView extends ChartView {
	private static final long serialVersionUID = -3970050001485966989L;

	private static List<WeakReference<PriceChartView>> priceChartViews = new ArrayList<WeakReference<PriceChartView>>();
	
	private PriceChart<? extends IBar> priceChart;
	private BarChart volumeChart;
	private DefaultAxis priceAxis;
	private DefaultAxis volumeAxis;
	private InfoAxis infoAxis;
	private DateAxis dateAxis;
	private Grid grid;
	private Crosshair crosshair;

	public PriceChartView(ChartingInternalFrameView chartingInternalFrameView) {
		super(ChartView.Type.PRICE, chartingInternalFrameView);
		priceChartViews.add(new WeakReference<PriceChartView>(this));
		this.grid = new Grid();
		this.crosshair = new Crosshair();
		getContentPane().add(grid, new Integer(Integer.MIN_VALUE));
		getContentPane().add(crosshair, new Integer(Integer.MAX_VALUE));
		PriceChart<? extends IBar> priceChart= new PriceChart<Bar>();
		setPriceChart(priceChart);
	
		setPriceAxis(new PriceAxis(this));
		setInfoAxis(new InfoAxis(this, priceChart.getModel()));
		setDateAxis(new DateAxis(this, priceChart.getModel()));
		setVolumeAxis(new DefaultAxis(this, Orientation.VERTICAL, BorderLayout.EAST));
		
		priceChart.addMouseMotionListener(infoAxis);
		priceChart.addMouseMotionListener(crosshair);
	}
	
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		grid.repaint();
	}
	
	@Override
	protected void finalize() throws Throwable {
		WeakReference<PriceChartView> removable = null;
		for(WeakReference<PriceChartView> ref : priceChartViews){
			if(ref.get() != null && ref.get().equals(this)){
				removable = ref;
				break;
			}
		}
		priceChartViews.remove(removable);
		super.finalize();
	}
	
	
	public static int getCount(){
		return priceChartViews.size();
	}
	


	public PriceChart<? extends IBar> getPriceChart() {
		return priceChart;
	}


	public void setPriceChart(PriceChart<? extends IBar> priceChart) {
		this.priceChart = priceChart;
		addChart(priceChart);
	}


	public BarChart getVolumeChart() {
		return volumeChart;
	}


	public void setVolumeChart(BarChart volumeChart) {
		this.volumeChart = volumeChart;
		addChart(volumeChart);
	}


	public DefaultAxis getPriceAxis() {
		return priceAxis;
	}


	public void setPriceAxis(DefaultAxis priceAxis) {
		this.priceAxis = priceAxis;
		addAxis(priceAxis);
	}


	public DefaultAxis getVolumeAxis() {
		return volumeAxis;
	}


	public void setVolumeAxis(DefaultAxis volumeAxis) {
		this.volumeAxis = volumeAxis;
		addAxis(volumeAxis);
	}


	public InfoAxis getInfoAxis() {
		return infoAxis;
	}


	public void setInfoAxis(InfoAxis infoAxis) {
		this.infoAxis = infoAxis;
		addAxis(infoAxis);
	}


	public DateAxis getDateAxis() {
		return dateAxis;
	}


	public void setDateAxis(DateAxis dateAxis) {
		this.dateAxis = dateAxis;
		addAxis(dateAxis);
	}
	
	public Grid getGrid() {
		return grid;
	}
	
}
